function EEG = cvc_event_convert2string(EEG)
% CVC_EVENT_CONVERT2STRING
%
%  Synopsis
%  ========
%
%  EEG = cvc_event_convert2string(EEG)
%
%  -- Author: Mads Dyrholm --
%     Center for Visual Cognition, University of Copenhagen.
%     June 2012
%
%  Purpose
%  =======
%
%  Convert numerical events into string events, according
%  to CVC_EVENT_INTELLIGENT.
%
%  Inputs
%  ======
%
%  EEG - EEGLAB data structure.
%
%  Outputs
%  =======
%
%  EEG.events - New event structure.

cvc_eventlist(EEG)

for ev=1:length(EEG.event)
    evtype = EEG.event(ev).type;
    if ~isnumeric(evtype), continue, end
    idx = find(evtype == EEG.cvc.edatevents.evtype);
    if ~isempty(idx)
        EEG.event(ev).type = EEG.cvc.edatevents.description{idx};
    else
        bbbbbb = bitand(evtype,63);
        switch bitshift(evtype,-14)
          case 0 % misc
            EEG.event(ev).type = ['M ' num2str(bbbbbb)];            
          case 1 % rbox
            EEG.event(ev).type = ['RBOX ' dec2bin(bbbbbb,6)];
          case 2 % timecode
            EEG.event(ev).type = ['tc'];
        end
    end
end
